package cn.maxwell._01_tran;

import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

/**
 * 事务监听器
 */
public class MyTransactionListener implements TransactionListener {
    /**
     * 执行本地事务
     * @param message
     * @param o
     * @return
     */
    @Override
    public LocalTransactionState executeLocalTransaction(Message message, Object o) {
        try {
            //int i = 1/0; // 用作测试事务机制
            System.out.println("本地事务执行成功");
            //我是一个参数哦，并且能背事物监听器接收!
            System.out.println(o);
            return LocalTransactionState.COMMIT_MESSAGE;//本地事务执行成功，提交
        } catch (Exception e) {
            e.printStackTrace();
        }
            return LocalTransactionState.ROLLBACK_MESSAGE;//本地事务未执行成功，回滚
    }

    /**
     * 检查本地事务状态
     * @param messageExt
     * @return
     */
    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
        try {
            System.out.println("本地事务执行成功");
            return LocalTransactionState.COMMIT_MESSAGE;//本地事务执行成功->提交
        } catch (Exception e) {
            e.printStackTrace();
        }
            return LocalTransactionState.ROLLBACK_MESSAGE;//本地事务未执行成功->回滚
    }
}
